package com.weilele.base.library

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.weilele.mvvm.base.helper.SingleClickListener
import com.weilele.mvvm.base.helper.createViewBinding
import com.weilele.mvvm.utils.activity.getResColor
import com.weilele.mvvm.utils.activity.getResDrawable
import com.weilele.mvvm.widget.BaseToolBar
import java.lang.IllegalArgumentException

class BaseHelperImpl<Binding : ViewBinding?> : IBaseHelperView<Binding> {

    private var _binding: Binding? = null

    override val mBinding: Binding
        get() {
            return _binding ?: throw IllegalArgumentException("binding is null")
        }

    private val baseHelperBean = IBaseHelperView.BaseHelperBean(null, null, null)
    private lateinit var self: Any
    private var rootView: View? = null

    /**
     * 创建toolbar
     */
    private fun getToolbar(context: Context, isNeedToolbar: Boolean): Toolbar? {
        return if (isNeedToolbar) {
            BaseToolBar(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
        } else {
            return null
        }
    }

    /**
     * 开始创建布局view
     */
    override fun getContentView(
        self: Any,
        rootView: View?,
        context: AppCompatActivity,
        layoutInflater: LayoutInflater,
        parent: ViewGroup?,
        isNeedToolbar: Boolean,
        replaceActionBar: Boolean,
        isNeedBackIcon: Boolean,
        isNeedSwitchView: Boolean,
    ): Any? {
        this.self = self
        //如果提供这个view，则优先使用此布局，binding将会失效，如果getBinging则会报错
        this.rootView = rootView
        val toolbar = getToolbar(context, isNeedToolbar)
        val switcherView = getSwitchView(context, layoutInflater, isNeedSwitchView)
        baseHelperBean.toolbar = toolbar
        baseHelperBean.switcherView = switcherView
        return if (toolbar == null) {
            switcherView ?: createBindingView(layoutInflater, parent)
        } else {
            //添加toolbar
            addToolbarView(context, layoutInflater, toolbar, switcherView, replaceActionBar)
                .apply { initDefaultToolbarUi(context, toolbar, isNeedBackIcon) }
        }
    }

    /**
     * 创建自定义的内容块，每个activity不一样
     */
    private fun createBindingView(inflater: LayoutInflater, parent: ViewGroup?): View? {
        val rootTemp = rootView
        if (rootTemp != null) {
            val lp = rootTemp.layoutParams
            if (lp == null) {
                rootTemp.layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            }
            return rootTemp
        }
        val binding = _binding ?: self.createViewBinding(inflater, parent = parent)
        _binding = binding
        return binding?.root
    }

    /**
     * 将toolbar添加到容器
     */
    private fun addToolbarView(
        context: AppCompatActivity, layoutInflater: LayoutInflater,
        toolbar: Toolbar,
        switcherView: SwitchView?,
        replaceActionBar: Boolean
    ): Any {
        if (replaceActionBar && context.supportActionBar == null) {
            //没有actionbar
            context.setSupportActionBar(toolbar)
        }
        return createToolbarParentView(context, layoutInflater, toolbar, switcherView)
    }


    /**
     * 在fragment中添加toolbar
     */
    private fun createToolbarParentView(
        context: AppCompatActivity,
        layoutInflater: LayoutInflater,
        toolbar: Toolbar,
        switcherView: SwitchView?,
    ): Any {
        val rootView = LinearLayout(context)
        rootView.id = View.generateViewId()
        baseHelperBean.rootView = rootView
        rootView.orientation = LinearLayout.VERTICAL
        rootView.addView(toolbar)
        if (switcherView != null) {
            rootView.addView(switcherView)
        } else {
            rootView.addView(createBindingView(layoutInflater, rootView))
        }
        return rootView
    }

    /**
     * 创建支持根据状态切换的view
     */
    private fun getSwitchView(
        context: Context,
        layoutInflater: LayoutInflater,
        isNeedSwitchView: Boolean
    ): SwitchView? {
        return if (isNeedSwitchView) {
            SwitchView(context) {
                createBindingView(layoutInflater, it) ?: View(it.context)
            }.apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            }
        } else {
            null
        }
    }

    /**
     * 初始化标题栏外观
     */
    private fun initDefaultToolbarUi(
        context: AppCompatActivity,
        toolbar: Toolbar,
        isNeedBackIcon: Boolean
    ) {
        toolbar.setBackgroundResource(R.color.toolBarBackground)
        if (isNeedBackIcon) {
            val drawable = context.getResDrawable(R.drawable.toolbarLogo)
            drawable?.setTint(context.getResColor(R.color.toolbarLogoTint))
            toolbar.navigationIcon = drawable
            toolbar.title = context.title
            toolbar.popupTheme = R.style.toolbarPopupTheme
            toolbar.setTitleTextAppearance(context, R.style.toolbarTitleTextAppearance)
            toolbar.setNavigationOnClickListener(SingleClickListener {
                context.onBackPressed()
            })
        }
    }

    override fun getBaseHelperBean(): IBaseHelperView.BaseHelperBean {
        return baseHelperBean
    }

    override fun getToolbar(): Toolbar? = baseHelperBean.toolbar

    override fun getSwitchView(): SwitchView? = baseHelperBean.switcherView

    override fun getRootView(): LinearLayout? = baseHelperBean.rootView
}